import cv2
import numpy as np
import matplotlib.pyplot as plt

img = cv2.imread("test1.jpg", cv2.IMREAD_UNCHANGED)
img = img[:,:,0]
cv2.imshow('original', img)
# 直方图
plt.figure(1)
hist = cv2.calcHist([img], [0], None, [256], [0, 256])
plt.plot(hist,label='hist')

# 均衡直方图
equ = cv2.equalizeHist(img)
cv2.imshow('equalized', equ)
hist = cv2.calcHist(equ, [0], None, [256], [0, 256])
plt.figure(2)
plt.plot(hist,label='均衡直方图')

# 自适应
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8,8))
cl = clahe.apply(img)
cv2.imshow('自适应', cl)
hist = cv2.calcHist(cl, [0], None, [256], [0, 256])
plt.figure(3)
plt.plot(hist,label='自适应均衡直方图')


plt.show()
cv2.waitKey(0)
cv2.destroyAllWindows()
